package com.haiya.common.spring;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * 应用程序上下文容器: 通过Spring的ApplicationContext来实现
 * 用于存取各种层的对象实例，例如获取存储器、领域服务等
 * @author qiaoguoqiang
 */
public class ApplicationContextProvider implements ApplicationContextAware {
    private static ApplicationContext applicationContext;

    public ApplicationContextProvider() {
        super();
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (ApplicationContextProvider.applicationContext == null) {
            ApplicationContextProvider.applicationContext = applicationContext;
        }
    }

    public static ApplicationContextProvider instance() {
        return (ApplicationContextProvider) applicationContext.getBean("applicationContextProvider");
    }

    public synchronized ApplicationContext applicationContext() {
        return ApplicationContextProvider.applicationContext;
    }
}
